<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0px;
            margin: 0px;
        }

        .wrapper {
            width: 500px;
            height: 300px;
            margin: 100px auto;
            overflow: hidden;
            position: relative;
        }

        .slider-page {
            list-style: none;
            width: 500%;
            height: 100%;
            position: absolute;
            left: 0px;
            top: 0px;
        }

        .clearfix::after {
            content: "";
            display: block;
            clear: both;
        }

        .slider-page li {
            float: left;
            width: 20%;
            height: 100%;
        }

        .slider-page li img {
            display: block;
            width: 500px;
            height: 300px;
            object-fit: cover;
        }

        .wrapper .btn {
            width: 40px;
            height: 40px;
            color: #fff;
            background-color: #000;
            text-align: center;
            line-height: 40px;
            position: absolute;
            margin: auto;
            opacity: 0.1;
            cursor: pointer;
        }

        .wrapper .btn:hover {
            opacity: 0.7;
        }

        .wrapper .leftbtn {
            left: 2px;
            top: 0px;
            bottom: 0px;
        }

        .wrapper .rightbtn {
            right: 2px;
            top: 0px;
            bottom: 0px;
        }

        .wrapper .sliderIndex {
            position: absolute;
            width: 100%;
            bottom: 15px;
            text-align: center;
            cursor: pointer;
        }


        .wrapper .sliderIndex span {
            width: 8px;
            height: 8px;
            background-color: #ccc;
            border-radius: 50%;
            display: inline-block;
            margin-right: 10px;
        }

        .wrapper .sliderIndex span.active {
            background-color: #999;
        }
    </style>
</head>
<body>

    <div class="wrapper">

        <ul class="slider-page clearfix">
            <li><a href=""><img src="./cat1.jpg" alt=""></a></li>
            <li><a href=""><img src="./cat2.jpg" alt=""></a></li>
            <li><a href=""><img src="./cat3.jpg" alt=""></a></li>
            <li><a href=""><img src="./cat4.jpg" alt=""></a></li>
            <li><a href=""><img src="./cat1.jpg" alt=""></a></li>
        </ul>

        <div class="btn leftbtn">&lt;</div>
        <div class="btn rightbtn">&gt;</div>
        <div class="sliderIndex">
            <span class="active"></span>
            <span></span>
            <span></span>
            <span></span>
        </div>


    </div>


    <script src="./move.js"></script>
    <script>

        var timer = null;
        var sliderPage = document.getElementsByClassName('slider-page')[0];
        var moveWidth = sliderPage.children[0].offsetWidth;
        var num = sliderPage.children.length - 1;
        // console.log(num);
        var leftBtn = document.getElementsByClassName('leftbtn')[0];
        var rightBtn = document.getElementsByClassName('rightbtn')[0];
        var spanArray = document.getElementsByClassName('sliderIndex')[0].getElementsByTagName('span');
        var lock = true;
        var index = 0;
        leftBtn.onclick = function () {
            autoMove('left->right');
        }
        rightBtn.onclick = function () {
            autoMove('right->left');
        }

        for(var i = 0; i < spanArray.length; i ++) {
            (function(i){
                spanArray[i].onclick = function() {
                    lock = false;
                    clearTimeout(timer);
                    index = i;
                    startMove(sliderPage, {left: -index * moveWidth}, function() {
                        timer = setTimeout(function () {
                            autoMove();
                        }, 2000);
                        //调用函数，改变小点的样式
                        changeIndex(index);
                        lock = true;
                    })
                }
            }(i));
        }


        // direction
        // 默认轮播方向/点击right按钮   'left->right' / undefined
        // 点击left按钮  'right->left'

        function autoMove(direction) {
            if (lock) {
                // 条件成立，从左到右轮播
                lock = false;
                clearTimeout(timer);
                if (!direction || direction == 'right->left') {
                    index ++;
                    startMove(sliderPage, { left: sliderPage.offsetLeft - moveWidth }, function () {
                        if (sliderPage.offsetLeft == - moveWidth * num) {
                            sliderPage.style.left = '0px';
                            index = 0;
                        }
                        timer = setTimeout(function () {
                            autoMove();
                        }, 2000);

                        //调用函数，改变小点的样式
                        changeIndex(index);

                        lock = true;
                    });
                } else if (direction == 'left->right') {
                    if (sliderPage.offsetLeft == 0) {
                        sliderPage.style.left = - moveWidth * num + 'px';
                        index = num;
                    }
                    index --;
                    startMove(sliderPage, { left: sliderPage.offsetLeft + moveWidth }, function () {
                        timer = setTimeout(function () {
                            autoMove();
                        }, 2000);
                        lock = true;
                        changeIndex(index);
                    });
                }
            }

        }

        
        function changeIndex(theIndex) {
            for(var i = 0; i < spanArray.length; i ++) {
                spanArray[i].className = "";
            }
            spanArray[theIndex].className = 'active';
        }


        timer = setTimeout(function () {
            autoMove();
        }, 2000);

    </script>
</body>
</html>